package one.bugu.android.demon.ui.activity.shop;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CalculationBean;
import one.bugu.android.demon.bean.MagicCalShopBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.activity.CalculationActivity;
import one.bugu.android.demon.ui.adapter.shop.MagicCalShopAdapter;
import one.bugu.android.demon.ui.refresh.SmartRefreshLayout;
import one.bugu.android.demon.ui.refresh.listener.OnRefreshLoadmoreListener;
import one.bugu.android.demon.ui.refresh.listener.RefreshLayout;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 魔法算力商城界面
 */
@LKContentView(R.layout.activity_magic_cal_shop)
public class MagicCalShopActivity extends MyBaseActivity {
    /**
     * topBar
     */
    @LKInjectView(R.id.btbv_qp_top_bar)
    private BaseTopBarView btbv_qp_top_bar;

    /**
     * 列表
     */
    @LKInjectView(R.id.lv_qp_list)
    private ListView lv_qp_list;

    /**
     * 缺省页
     */
    @LKInjectView(R.id.ll_empty_view)
    private LinearLayout ll_empty_view;

    /**
     * 刷新控件
     */
    @LKInjectView(R.id.ref_layout)
    private SmartRefreshLayout ref_layout;

    /**
     * 数据适配器
     */
    private MagicCalShopAdapter mAdapter;

    private String token;

    @Override
    protected void initView() {
        super.initView();

        StatusBarUtils.getInstance().setStatusBar(MagicCalShopActivity.this, btbv_qp_top_bar);

        mAdapter = new MagicCalShopAdapter(this, null);
        lv_qp_list.setAdapter(mAdapter);

        ref_layout.setEnableRefresh(false);//启用刷新
        ref_layout.setEnableLoadmore(false);//启用加载
    }

    @Override
    protected void initData() {
        super.initData();
        token = PreferencesUtil.getInstance().getString(MagicCalShopActivity.this, Constant.TOKEN);
        getData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //topBar事件处理
        btbv_qp_top_bar.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {
                IntentUtils.startAty(MagicCalShopActivity.this, OrderManagerActivity.class);
            }
        });

        //刷新加载
        ref_layout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //加载数据
                ref_layout.finishLoadmore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //刷新数据
                ref_layout.finishRefresh();
            }
        });
    }

    /**
     * 获取数据
     */
    private void getData(){
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.MAGIC_CAL_SHOP, map, handler);
    }

    /**
     * 获取算力信息的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<MagicCalShopBean>> handler = new BaseHttpAsycResponceHandler<List<MagicCalShopBean>>(true) {
        @Override
        public void onSuccess(List<MagicCalShopBean> calculationBeans) {
            super.onSuccess(calculationBeans);
            if(calculationBeans != null && !calculationBeans.isEmpty()){
                mAdapter.addAll((ArrayList<MagicCalShopBean>) calculationBeans);
            }else{
                setEmptyView();
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            setEmptyView();
        }

        @Override
        public void onError() {
            super.onError();
            setEmptyView();
        }
    };



    /**
     * 设置显示缺省页
     */
    private void setEmptyView() {
        boolean showEmptyView = mAdapter != null && mAdapter.getCount() > 0;
        ref_layout.setVisibility(showEmptyView ? View.VISIBLE : View.GONE);
        ll_empty_view.setVisibility(showEmptyView ? View.GONE : View.VISIBLE);
    }

}
